import {
  createRouter,
  createWebHistory,
  createWebHashHistory
} from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { useUserStore, useListStore } from '@/stores'
// createRouter 创建路由实例，===> new VueRouter()
// history 是路由模式，hash模式，history模式
// createWebHistory() 是开启history模块   http://xxx/user
// createWebHashHistory() 是开启hash模式    http://xxx/#/user

// vite 的配置 import.meta.env.BASE_URL 是路由的基准地址，默认是 ’/‘
// https://vitejs.dev/guide/build.html#public-base-path
// 如果将来你部署的域名路径是：http://xxx/my-path/user
// vite.config.ts  添加配置  base: my-path，路由这就会加上 my-path 前缀了

const router = createRouter({
  // createWebHistory(import.meta.env.BASE_URL),

  history: createWebHashHistory(),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/login/index.vue'),
      meta: { title: '登录页' }
    },
    {
      path: '/',
      redirect: '/home',
      component: () => import('@/views/Layout/index.vue'),
      children: [
        {
          path: '/home',
          component: () => import('@/views/Home/index.vue'),
          meta: { title: '首页' }
        },
        {
          path: '/Usecheck',
          component: () => import('@/views/Usecheck/index.vue'),
          meta: { title: '我要查' }
        },
        {
          path: '/article',
          component: () => import('@/views/Article/index.vue'),
          meta: { title: '我要学' }
        },
        {
          path: '/notify',
          component: () => import('@/views/Notify/index.vue'),
          meta: { title: '我要办' }
        },

        {
          path: '/user',
          component: () => import('@/views/User/index.vue'),
          meta: { title: '个人中心' }
        }
      ]
    },
    {
      path: '/VideoDisplay',
      component: () => import('@/views/VideoDisplay/index.vue'),
      meta: { title: '视频播放' }
    },
    {
      path: '/Announcement',
      component: () => import('@/views/Announcement/index.vue'),
      meta: { title: '公告查询' }
    },
    {
      path: '/Nestedpage',
      component: () => import('@/views/Nestedpage/index.vue'),
      meta: { title: '嵌套页面' }
    },
    {
      path: '/Picture',
      component: () => import('@/views/Picture/index.vue'),
      meta: { title: '操作指南' }
    },
    {
      path: '/Feedback',
      component: () => import('@/views/Feedback/index.vue'),
      meta: { title: '咨询记录' }
    },
    {
      path: '/AppList',
      component: () => import('@/views/AppList/index.vue'),
      meta: { title: '访问记录' }
    },
    {
      path: '/Appsearch',
      component: () => import('@/views/Appsearch/index.vue'),
      meta: { title: '应用搜索' }
    },
    {
      path: '/404',
      component: () => import('@/views/404/index.vue'),
      meta: { title: '报错页' }
    }
  ]
})
//前置路由守卫
router.beforeEach((to) => {
  //开启进度条
  // NProgress.start()
  //获取token
  const store = useUserStore()
  //白名单
  const whiteList = ['/login', '/404']

  //判断是否登录且不在白名单中
  if (!store.user && !whiteList.includes(to.path)) {
    return '/login'
  } else if (store.user && whiteList.includes(to.path)) {
    return '/'
  }
})
//后置路由守卫
router.afterEach((to) => {
  const setList = useListStore()
  //关闭进度条
  NProgress.done()
  //修改页面标题
  document.title = `${to.meta.title || ''}-${setList.sysinfo.sysName}`
})
export default router
